﻿using System;
using System.Net;
using System.IO;
using Liber;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using System.Text.Encodings;
using System.Text;

namespace FlashQ.Main
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#if DEBUG
            args = new string[1];
            args[0] = "onworking0x";
#endif
            Liber.Runtime.LiberRuntime.Run(args, null, () =>
            {
                var urls = XMLSetting.DefaultSetting.First("WebHost").All("Url", false);

                WebHost.CreateDefaultBuilder(args)
                        .UseKestrel(opt =>
                        {
                            foreach (var u in urls)
                            {
                                string ssl = u.Property("SSL", "");
                                IPAddress ip = IPAddress.Parse(u.Value);

                                if (ssl.Length > 0)
                                {
                                    try
                                    {
                                        opt.Listen(ip, int.Parse(u.Property("Port")), ln =>
                                        {
                                            ln.UseHttps(Path.IsPathRooted(ssl) ? ssl : (AppDomain.CurrentDomain.BaseDirectory + "\\" + ssl), u.Property("SSLPwd"), (cfg) =>
                                            {
                                                cfg.ClientCertificateMode = Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode.NoCertificate;
                                                cfg.SslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls11 | System.Security.Authentication.SslProtocols.Tls;

                                            });
                                            ln.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http1AndHttp2;
                                        });
                                    }
                                    catch (Exception ex)
                                    {
                                        throw new SystemException("无法加载SSL证书");
                                    }
                                }
                                else if (IPAddress.IsLoopback(ip))
                                {
                                    opt.ListenLocalhost(int.Parse(u.Property("Port")));
                                }
                                else
                                {
                                    opt.Listen(ip, int.Parse(u.Property("Port")));
                                }
                            }
                        })
#if DEBUG
                        .UseWebRoot(".\\Web")
#else
                        .UseWebRoot("Web")
#endif
                        .UseStartup<Startup>()
                        .Build()
                        .Run();
            });
        }
    }
}
